﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using Pharmacy.Data;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;
using Pharmacy.Entities;

public partial class Admin_PreviewRevenue : System.Web.UI.Page
{
    DateTime tmpfromdate, tmptodate, fromdate, todate;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["From"] != null && Request.QueryString["To"] != null)
                {
                    tmpfromdate = DateTime.Parse(Request.QueryString["From"].ToString());
                    tmptodate = DateTime.Parse(Request.QueryString["To"].ToString());
                    fromdate = new DateTime(tmpfromdate.Year, tmpfromdate.Month, tmpfromdate.Day, 0, 0, 0);
                    todate = new DateTime(tmptodate.Year, tmptodate.Month, tmptodate.Day, 23, 59, 59);
                    PreviewRevenue(fromdate, todate);

                }
            }
            catch (Exception)
            {

            }
        }
    }
    private void PreviewRevenue(DateTime from, DateTime to)
    {
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "_Revenue_GetByDate";
        cmd.Parameters.Add("@fromdate", SqlDbType.DateTime).Value = from;
        cmd.Parameters.Add("@todate", SqlDbType.DateTime).Value = to;
        ds = DataRepository.Provider.ExecuteDataSet(cmd);
        DataTable ntablePre = ds.Tables[0];
        DataTable ntableEA = ds.Tables[1];
        DataTable ntableR = ds.Tables[2];

        decimal totalPre = 0;
        decimal amountPre = 0;
        decimal amountEA = 0;
        decimal totalEA = 0;
        decimal amountRG = 0;
        decimal totalRG = 0;
        DSRevenue.PrescriptionDataTable dtPre = new DSRevenue.PrescriptionDataTable();
        DSRevenue.PrescriptionRow dtPrescriptionRow;
        for (int i = 0; i < ntablePre.Rows.Count; i++)
        {
            dtPrescriptionRow = dtPre.NewPrescriptionRow();
            DataRow drw = ntablePre.Rows[i];
            dtPrescriptionRow.PrescriptionID = drw["PrescriptionID"].ToString();
            dtPrescriptionRow.PatientCode = drw["PatientCode"].ToString();
            dtPrescriptionRow.PatientName = drw["PatientName"].ToString();
            dtPrescriptionRow.VAT = decimal.Parse(drw["VAT"].ToString());
            dtPrescriptionRow.Amount = decimal.Parse(drw["Amount"].ToString());
            dtPrescriptionRow.TotalAmount = decimal.Parse(drw["TotalAmount"].ToString());
            dtPrescriptionRow.SalesPerson = drw["SalesPerson"].ToString();
            dtPre.AddPrescriptionRow(dtPrescriptionRow);
            //amountPre = decimal.Parse(drw["TotalAmount"].ToString());
            //totalPre += amountPre;
        }
        //exportadjust
        DSRevenue.ExportAdjustDataTable dtEA = new DSRevenue.ExportAdjustDataTable();
        DSRevenue.ExportAdjustRow dtEARow;
        for (int i = 0; i < ntableEA.Rows.Count; i++)
        {
            dtEARow = dtEA.NewExportAdjustRow();
            DataRow drw = ntableEA.Rows[i];
            dtEARow.StockOutId = drw["StockOutId"].ToString();
            dtEARow.StockOutDate = drw["StockOutDate"].ToString();
            dtEARow.TotalAmount = decimal.Parse(drw["TotalAmount"].ToString());
            dtEARow.PreparedBy = drw["PreparedBy"].ToString();
            dtEARow.Remark = drw["Remark"].ToString();
            dtEA.AddExportAdjustRow(dtEARow);
            //amountEA = decimal.Parse(drw["TotalAmount"].ToString());
            //totalEA += amountEA;
        }
        //return goods
        DSRevenue.ReturnGoodsDataTable dtRG = new DSRevenue.ReturnGoodsDataTable();
        DSRevenue.ReturnGoodsRow dtRGRow;
        for (int i = 0; i < ntableR.Rows.Count; i++)
        {
            dtRGRow = dtRG.NewReturnGoodsRow();
            DataRow drw = ntableR.Rows[i];
            dtRGRow.StockOutId = drw["StockOutId"].ToString();
            dtRGRow.StockOutDate = drw["StockOutDate"].ToString();
            dtRGRow.TotalAmount = decimal.Parse(drw["TotalAmount"].ToString());
            dtRGRow.PreparedBy = drw["PreparedBy"].ToString();
            dtRGRow.Remark = drw["Remark"].ToString();
            dtRG.AddReturnGoodsRow(dtRGRow);
            //amountRG = decimal.Parse(drw["TotalAmount"].ToString());
            //totalRG += amountRG;
        }
        DSRevenue.TitleDataTable dtTitle = new DSRevenue.TitleDataTable();
        DSRevenue.TitleRow dtTitleRow = dtTitle.NewTitleRow();
        dtTitleRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtTitleRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;
        dtTitleRow.TotalAmountPre = totalPre;
        dtTitleRow.TotalAmountEA = totalEA;
        dtTitleRow.TotalAmountRG = totalRG;
        //get prepareby from cookies
        var httpCookie = Request.Cookies["LoginUser"];
        WebUser id = DataRepository.WebUserProvider.GetByUserId(httpCookie.Value);
        dtTitleRow.PrepareBy = id.Name;
        dtTitle.AddTitleRow(dtTitleRow);

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSRevenue_Prescription", dtPre));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSRevenue_Title", dtTitle));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSRevenue_ExportAdjust", dtEA));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSRevenue_ReturnGoods", dtRG));
        ReportViewer1.DataBind();
    }
}
